import {inBrowser} from '@utils'

let maxToastId = 0

const state = {
  loading: false,
  progress: 0,
  showLoginModal: false,
  showRegisterModal: false,
  toast: [],
  loading: false
}

const actions = {
  ['gProgress']({commit}, payload) {
    commit('progress', payload)
  },
  ['toast'] ({commit}, text) {
    let id = ++ maxToastId;
    commit('addToast', {id, text: text})
    setTimeout(() => commit('removeToast', id), 2000)
  },
  ['setLoading'] ({commit}, isShow) {
    commit('loading', isShow)
  }
}

const mutations = {
  ['progress'](state, payload) {
    state.progress = payload
  },
  ['addToast'] (state, message) {
    state.toast.push(message)
  },
  ['removeToast'] (state, id) {
    state.toast = state.toast.filter(m => m.id !== id)
  },
  ['loading'] (state, isShow) {
    return state.loading = isShow;
  }
}

const getters = {
  ['getGlobal'](state) {
    return state
  },
  ['loading'] (state) {
    return state.loading;
  }
}

export default {
  actions,
  state,
  mutations,
  getters
}
